/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-13 11:00:08
 * @LastEditTime: 2025-10-14 15:29:10
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\set_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include "handheld.h"
#include "bsp_lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

static lv_obj_t *slider_brightness; //亮度调节滑块

static int brightness_value = 100;

LV_FONT_DECLARE(font_alipuhui20);

/**
 * @description: 返回主界面向上滑动事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void my_gesture_event_cb(lv_event_t *e)
{
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    if (dir == LV_DIR_TOP)
    {
        lv_obj_del(icon_in_obj); //删除画布
        icon_flag = 0;
    }
}

/**
 * @description: 滑动部件事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void slider_event_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    brightness_value = lv_slider_get_value(obj);
    bsp_display_brightness_set(brightness_value);
}

/**
 * @description: 设置应用
 * @return {*}
 */
void set_app(void)
{
    lvgl_port_lock(0);

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0xE680FF));
    lv_style_set_bg_grad_color(&style, lv_color_hex(0xE68000));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);
    
    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 创建文本提示
    lv_obj_t *label_text = lv_label_create(icon_in_obj);
    lv_label_set_text(label_text, "滑动调节屏幕亮度");
    lv_obj_set_style_text_color(label_text, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_text, &font_alipuhui20, 0);
    lv_obj_align(label_text, LV_ALIGN_CENTER, 0, -80);

    // 创建亮度调节滑块
    slider_brightness = lv_slider_create(icon_in_obj);
    lv_obj_center(slider_brightness);
    lv_slider_set_range(slider_brightness, 20, 100);
    lv_slider_set_value(slider_brightness, brightness_value, LV_ANIM_OFF);
    lv_obj_set_height(slider_brightness, 50);
    lv_obj_add_event_cb(slider_brightness, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    // 返回提示符
    lv_obj_t *label_back = lv_label_create(icon_in_obj);
    lv_label_set_text(label_back, LV_SYMBOL_UP);
    lv_obj_set_style_text_color(label_back, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_BOTTOM_RIGHT, -20, -20);

    // 添加向上滑动退出功能
    lv_obj_add_event_cb(icon_in_obj, my_gesture_event_cb, LV_EVENT_GESTURE, 0);
    lv_obj_clear_flag(icon_in_obj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_flag(icon_in_obj, LV_OBJ_FLAG_CLICKABLE);

    lvgl_port_unlock();
    
    icon_flag = 12;
}